define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'share/cash/index' + location.search,
                    add_url: 'share/cash/add',
                    //edit_url: 'share/cash/edit',
                    //del_url: 'share/cash/del',
                    batchaudit_url: 'share/cash/batchaudit',
                    batchrefuse_url: 'share/cash/batchrefuse',
                    multi_url: 'share/cash/multi',
                    import_url: 'share/cash/import',
                    table: 'share_cash',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'users.avatar_url', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'users.account', title: __('Users.account'), operate: 'LIKE'},
                        {field: 'users.realname', title: __('Users.realname'), operate: 'LIKE'},
                        { title: __('账号信息'), formatter:function (value, row, index) {
                            var html='';
                            if (row.type==0){
                                html += "<div>姓名:"+row.name+"</div>";
                                html += "<div>微信昵称:"+row.mobile+"</div>";
                            } else if(row.type==1){
                                html += "<div>姓名:"+row.name+"</div>";
                                html += "<div>微信昵称:"+row.mobile+"</div>";
                            } else if(row.type==2){
                                html += "<div>姓名:"+row.name+"</div>";
                                html += "<div>开户行:"+row.bank_name+"</div>";
                                html += "<div>银行卡号:"+row.mobile+"</div>";
                            } else if(row.type==3){
                                html += "<div>余额提现</div>";
                            }
                            return html;
                        }, operate:false},
                        {field: 'price', title: __('用户申请提现金额'), operate:'BETWEEN'},
                        {field: 'service_money', title: __('手续费'), operate:false},
                        {field: 'money', title: __('实际打款金额'), operate:false},
                        {field: 'status', title: __('申请状态'), formatter: Table.api.formatter.normal, searchList:{1: __('申请中'),  2: __('已打款 '), 3: __('驳回 ')}},
                        {field: 'createtime', title: __('提交时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'audit',
                                    text: __('通过'),
                                    confirm:'确认通过吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'share/cash/audit',
                                    visible:function(row){
                                        if(row.status == 1){
                                            return true;
                                        }
                                    }
                                },{
                                    name: 'refuse',
                                    text: __('驳回'),
                                    confirm:'确认驳回吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-danger btn-ajax',
                                    url: 'share/cash/refuse',
                                    visible:function(row){
                                        if(row.status == 1){
                                            return true;
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'share/cash/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'share/cash/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'share/cash/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
